package com.xhui.vimarket.zkplus.zul.attr.mod.impl;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Span;

import com.xhui.crm.common.Constants;
import com.xhui.crm.common.util.date.DateUtil;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.ui.id.ZKRebcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusls;
import com.xhui.vimcom.xhui.vimarket.zkplusil.ZkUiUtil;
import com.xhcom.xhui.vimarket.zkpluslus.zul.attr.AttrExt;
import comcom.xhui.vimarket.zkpluszkplus.zul.attr.bean.AttrInst;
icom.xhui.vimarket.zkplusvimarket.zkplus.zul.attr.bean.Acom.xhui.vimarket.zkplust com.xhui.vimarket.zkplus.zul.attr.bean.com.xhui.vimarket.zkplus com.xhui.vimarket.zkplus.zul.attr.mcom.xhui.vimarket.zkplustants;
import com.xhui.vimarket.zkplus.zucom.xhui.vimarket.zkplusCompUtil;
import com.xhui.vimarket.zkplcom.xhui.vimarket.zkpluse.AttrValueFromRule;
import com.xhui.com.xhui.vimarket.zkplus.zul.attr.rule.AttrValueToRule;
import com.xhui.vimarket.zkplus.zucom.xhui.vimarket.zkplusrConstants;
import com.xhui.vimarket.zkplus.zul.attr.util.AttrUtil;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDatetimeComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrDatetimeComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrDatetimeComp inst                 = new AttrDatetimeComp();
    
    /**
     * 日志.
     */
    private static Logger           logger               = Logger.getLogger(AttrDatetimeComp.class);
    
    /**
     * .
     */
    private static String           TIME_FORMAT_HH_MM_SS = "HH:mm:ss";
    
    /**
     * .
     */
    private static String           TIME_FORMAT_HH_MM    = "HH:mm";
    
    /**
     * 构造函数. 
     */
    private AttrDatetimeComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrDatetimeComp instance() {
        return AttrDatetimeComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // panel
        final Hbox hbox = new Hbox();
        hbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        hbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        hbox.setAttribute(AttrBaseComp.COMP_CANTNULL, AttrCompUtil.getStrCantNull(attrTmpl
            .isCantNull()));
        hbox.setAttribute(AttrBaseComp.COMP_JAVACODE, attrTmpl.getJavaCode());
        hbox.setAttribute(AttrBaseComp.COMP_COMP_NAME, attrTmpl.getName());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_LENGTH, attrTmpl.getValueLength());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_FROM, attrTmpl.getValueFrom());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_TO, attrTmpl.getValueTo());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_REX, attrTmpl.getValidExpr());
        hbox.setWidth("100%");
        hbox.setSclass("zh-hbox-widths");
        hbox.setWidths(this.getWidthsLCT(attrTmpl));
        final Div labDiv = new Div();
        labDiv.setWidth("100%");
        final Label label = new Label(attrTmpl.getName());
        label.setSclass("zp-lt-label");
//        final String sDateboxWithComposer = attrExt.getAttrAda().getAttrTmplCompStr(
//            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
//        final Datebox datebox = (Datebox) Executions.createComponentsDirectly(sDateboxWithComposer,
//            null, null, null);
        final Datebox datebox = (Datebox) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, null);
        //重构Uuid用于压力测试。
        ZKRebuildUuid.rebuildUuidById(datebox, "db_"
            + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                : "_" + Math.abs(attrTmpl.getAttrId())));
        datebox.setFormat("yyyy-MM-dd " + TIME_FORMAT_HH_MM);
        datebox.setWidth(AttrBaseComp.WIDTH_SELECT);
        if (attrExt.getAttrAda().isViewMode()) { // 展示模式
            datebox.setDisabled(true);
        }
        if (attrTmpl.getHint() != null) {
            hbox.setTooltiptext(attrTmpl.getHint());
        }
        if (!attrTmpl.isDisplay()
            || AttrConstants.DISPLAY_STYLE_PANEL_HIDE.equals(attrTmpl.getDisplayStyle())) {
            hbox.setVisible(false); // 隐藏
        }
        // info
        final Span infDiv = this.createRulebar(attrExt, labDiv, attrTmpl);
        // add to zul
        hbox.setParent(comp);
        labDiv.setParent(hbox);
        label.setParent(labDiv);
        infDiv.setParent(labDiv);
        datebox.setParent(hbox);
        // toolbox
        this.createToolbox(hbox, attrExt.isToolboxVisible(), attrTmpl);
        // comp log
        if (AttrDatetimeComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"40%,55%,5%\">");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerComp("<label value=\"");
            attrExt.appendInnerComp(attrTmpl.getName());
            attrExt.appendInnerComp("\" />");
            attrExt.appendInnerComp("<datebox format=\"yyyy-MM-dd HH:mm\" />");
            attrExt.appendInnerComp("</hbox>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList,Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList,attrViewMap);
        // 取输入框值
        final Component compDatebox = getDatebox(comp);
        if (compDatebox != null) {
            final long theAttrId = NumUtils
                .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
            final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                theAttrId);
            if (theAttrList.size() > 0 && !StrUtil.isNullOrEmpty(theAttrList.get(0).getParam1())) {
                // 健壮性需要再补充，待补充             
                String dateStr = theAttrList.get(0).getParam1();
                if (!StrUtil.isNullOrEmpty(dateStr) && (dateStr.length() == 14)) {
                    dateStr = DateUtil.datetimeStr14To19(dateStr);
                    ((Datebox) compDatebox).setText(theAttrList.get(0).getParam1());
                } else if (!StrUtil.isNullOrEmpty(dateStr)) {
                    ((Datebox) compDatebox).setText(dateStr);
                } else {
                    ((Datebox) compDatebox).setText("");
                }
                // javacode回填
                AttrCompUtil.copyTmplInfo(theAttrList.get(0), comp);
            }
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 取输入框值
        final Component compDatebox = getDatebox(comp);
        if (compDatebox != null) {
            if (!"".equals(((Datebox) compDatebox).getText())) { // 有填写
                final AttrInst attrInst = attrExt.newAttrInst();
                attrInst.setAttrId(NumUtils
                    .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
                attrInst
                    .setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
                attrInst.setParam1(getStrTimeFormat(((Datebox) compDatebox).getText(),
                    TIME_FORMAT_HH_MM));
                attrInst.setOp(this.getToolbarOp(comp));
        com.xhui.vimarket.zkplus.add(attrInst);
            }
        }
  com.xhui.vimarket.zkplustList;
    }

    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        if (AttrCompUtil.isCantNull(comp)) { // 必填
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) {
                ValiVo retVo = new ValiVo();
                ArrayList<ValiVo> retVoList = new ArrayList<ValiVo>();
                retVoList.add(retVo);
                return retVoList;
            }
        }
        return null;
    }
    
    /**
     * getStrTimeFormat.
     * 
     * @param val String
     * @param format String
     * @return String
     * @author starain
     * 2011-4-8 starain 
     */
    private static String getStrTimeFormat(String val, String format) {
        if (StrUtil.isNullOrEmpty(val) || StrUtil.isNullOrEmpty(format)) {
            return "";
        } else if (TIME_FORMAT_HH_MM_SS.equals(format)) {
            return val;
        } else if (TIME_FORMAT_HH_MM.equals(format)) {
            return val + ":00";
        }
        return val;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        final AttrInst attrInst = attrExt.newAttrInst();
        attrInst.setAttrId(attrTmpl.getAttrId());
        attrInst.setJavaCode(attrTmpl.getJavaCode());
        retList.add(attrInst);
        return retList;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        super.clean(attrExt, comp);
        // 取输入框值
        final Component compDatebox = getDatebox(comp);
        if (compDatebox != null) {
            ((Datebox) compDatebox).setText("");
        }
    }
    
    /**
     * getDatebox.
     * 
     * @param root Component
     * @return Datebox
     */
    public static Datebox getDatebox(Component root) {
        // 取输入框
        // hbox // Datebox(1) 
        return (Datebox) root.getFirstChild().getNextSibling();
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
        super.setDisabled(attrExt, comp, tflag);
        ZkUiUtil.setDisabled(comp, tflag);
    }
    
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
        if (valList == null || valList.size() < 1) { // 没有值
            if (AttrCompUtil.isCantNull(comp)) { // 需限制不可空
                return AttrCompUtil.getRetVoList(AttrCompConstants.RET_ERROR, AttrCompUtil
                    .getCompName(comp)
                    + AttrCompConstants.STRING_CANT_NULL, comp);
            }
            return null; // 通过
        } else { // 其他规则验证
            RetVO fromRetVo = AttrValueFromRule.judgeDatetime(valList, comp);
            if (!Constants.RET_TRUE.equals(fromRetVo.getResult())) {
                return AttrUtil.tranRetVoToOneList(fromRetVo);
            }
            RetVO toRetVo = AttrValueToRule.judgeDatetime(valList, comp);
            if (!Constants.RET_TRUE.equals(toRetVo.getResult())) {
                return AttrUtil.trcom.xhui.vimarket.zkplust(toRetVo);
            }
        }
        return null; // 通过
    }
    
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */
    public void setAttrView(final Component comp, Map<String, Integer> attrViewMap) {
        if (attrViewMap != null && attrViewMap.size() > 0) {
            String attrJC = comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "";     
            if (!StrUtil.isNullOrEmpty(attrJC)) {
                Integer viewType = attrViewMap.get(attrJC);
                // 取输入框
                final Datebox datebox = getDatebox(comp);
                if (datebox != null) {
                    datebox.setVisible(false);
                    if (AttrBaseComp.VIEW_TYPE_SHOW == viewType) {
                        datebox.setDisabled(true);
                        datebox.setVisible(true);
                    } else if (AttrBaseComp.VIEW_TYPE_EDIT == viewType) {
               com.xhui.vimarket.zkplusx.setVisible(true);
                    }
                }
            }
        }
    }

    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getMsgComp(org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-7-19 wuyx
     */
    @Override
    public Component getMsgComp(Component comp) {
        return getDatebox(comp);
    }
}
